<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\api\service\User as UserService;
use app\common\model\ding\Yuyue as YuyueModel;
use cores\exception\BaseException;

/**
 * 模型
 * Class Article
 * @package app\api\model
 */
class Yuyue extends YuyueModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'update_time',
	];

	/**
	 * 获取文章详情并累计阅读次数
	 * @param int $articleId 文章ID
	 * @return static|null
	 * @throws BaseException
	 */
	public static function getDetail(int $orderId) {
		
		$detail = parent::detail($orderId);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前预订不存在');
		}

		return $detail;
	}

	public function cancel() {

		$this->save(['status' => 30]);
		return true;
	}
	//确认客到
	public function queu() {

		$this->save(['status' => 20]);
		return true;
	}
	public function add(array $data) {
		// 当前用户信息
		$user = UserService::getCurrentLoginUser(true);

		// 添加
		return $this->transaction(function () use ($user, $data) {

			$data = array_merge($data, ['user_id' => $user['user_id'],'mobile'=>$user['mobile'],
				'store_id' => self::$storeId]);
			$this->save($data);

		

			return true;
		});
	}

	public function getListAll($param) {
		$params = $this->setQueryDefaultValue($param, [

			'type' => -1,
			'categoryId' => -1,
		]);
		// 检索查询条件
		$filter = [];

		$params['categoryId'] > 0 && $filter[] = ['category_id', '=', $params['categoryId']];
		$params['type'] > 0 && $filter[] = ['type', '=', $params['type']];
		//!empty($params['date']) && $filter[] = ['date', '=', $params['date']];
		// 获取列表数据
		return $this->with(['clerk', 'letters', 'shop.logoImage'])

			->where($filter)

			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();
	}
	public function getList($param) {
		$params = $this->setQueryDefaultValue($param, [

			'keyword' => '',
			'status' => -1,
			'categoryId' => -1,
		]);
		// 检索查询条件
		$filter = [];

		$params['categoryId'] > 0 && $filter[] = ['category_id', '=', $params['categoryId']];
		$params['status'] > 0 && $filter[] = ['status', '=', $params['status']];
		!empty($params['keyword']) && $filter[] = ['mobile', '=', $params['keyword']];
		//!empty($params['date']) && $filter[] = ['date', '=', $params['date']];
		// 获取列表数据
		return $this->with(['clerk'])

			->where($filter)

			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->paginate(15);
	}
}